using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;

public class MonsterSpawner : MonoBehaviour
{
    public StageMonsterData monsterdata;
    public PlayerCharacterController player;


    public GameController game;

    public MonsterController mstHead;
    public Image mstHeadProgress;
    public Vector3 mstHeadBorn;
    public float mstHeadView;

    private float _time;
    public float birthMoment;

    public int isExist;


    private void Awake()
    {
        //Debug.Log(GameData.archiveData.stage);

        //LoadMonster();
    }
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (isExist == 1)
        {
            mstHeadProgress.fillAmount = mstHead.Hp;
            if (mstHead.Hp <= 0)
            {
                //StartCoroutine("NextStage");
                isExist = 2;
            }
            return;
        }
        if (isExist == 0)
        {
            LayerMask playerLayer = 1 << LayerMask.NameToLayer("Player");

            Collider[] colliders = Physics.OverlapSphere(mstHeadBorn, mstHeadView, playerLayer);
            for (int i = 0; i < colliders.Length; i++)
            {
                player = colliders[i].gameObject.GetComponent<PlayerCharacterController>();
            }

            mstHeadProgress.fillAmount = _time / birthMoment;

            if (player != null)
            {
                if ((player.transform.position - mstHeadBorn).sqrMagnitude > mstHeadView * mstHeadView)
                    player = null;
                _time += Time.deltaTime;
                mstHeadProgress.enabled = true;
                if (_time > birthMoment)
                {
                    GameObject boss = Instantiate(Resources.Load<GameObject>("Monster/" + monsterdata.bossName));
                    boss.GetComponent<NavMeshAgent>().Warp(mstHeadBorn);
                    mstHead = boss.GetComponent<MonsterController>();
                    mstHead.home = mstHead.transform.position;
                    isExist = 1;
                }
            }
            else mstHeadProgress.enabled = false;

        }


    }

    public void Refresh(Vector3 born)
    {
        if (isExist == 0)
        {
            _time += 10;
        }
        StartCoroutine(Monster(born));
    }

    IEnumerator Monster(Vector3 born)
    {
        for (int i = 0; i < monsterdata.stageMonsters.Count; i++)
        {
            if (monsterdata.stageMonsters[i].born == born)
            {
                string mstname = "Monster" + monsterdata.stageMonsters[i].type;
                yield return new WaitForSeconds(monsterdata.stageMonsters[i].refreshCD);
                GameObject mst = Instantiate(Resources.Load<GameObject>("Monster/" + mstname));
                mst.GetComponent<NavMeshAgent>().Warp(monsterdata.stageMonsters[i].born);
            }
        }


    }

    IEnumerator NextStage()
    {
        GameData.PassLevel();
        yield return new WaitForSeconds(3);
        LoadMonster();
    }

    public void LoadMonster()
    {
        _time = 0;
        //monsterdata = Resources.Load<StageMonsterData>("Monster/Stage/Stage" + GameData.archiveData.stage);
        mstHeadBorn = monsterdata.bossBorn;
        mstHeadView = monsterdata.bossView;
        birthMoment = monsterdata.timeOfAppearance;
        for (int i = 0; i < monsterdata.stageMonsters.Count; i++)
        {
            string mstname = "Monster" + monsterdata.stageMonsters[i].type;
            GameObject mst = Instantiate(Resources.Load<GameObject>("Monster/" + mstname));
            mst.GetComponent<NavMeshAgent>().Warp(monsterdata.stageMonsters[i].born);
        }
        isExist = 0;
    }


}
